package com.chenliang.service.impl;

import com.chenliang.dao.IUsersInfo;
import com.chenliang.domain.Role;
import com.chenliang.domain.UserInfo;
import com.chenliang.service.IUserInfoService;
import com.chenliang.utils.BCryptPasswordEncoderUrils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ProjectName: mavenSSM
 * @Package: com.chenliang.service.impl
 * 作用：
 */
@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private IUsersInfo usersInfo;

    @Override
    public List<UserInfo> findall(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<UserInfo> userInfos = usersInfo.findall();
        return userInfos;
    }
//添加用户，并把用户的密码加密
    @Override
    @Transactional
    public void addUser(UserInfo users) {
        String encodePassword = BCryptPasswordEncoderUrils.encodePassword(users.getPassword());
        users.setPassword(encodePassword);
        usersInfo.addUser(users);

    }
//用户详情
    @Override
    public UserInfo findById(String id) {

        return  usersInfo.findById(id);
    }
//查询用户还没有的权限
    @Override
    public List<Role> userAddRole(String id) {
      return usersInfo.findByIdWithoutRole(id);
    }
//用户添加权限
    @Override
    @Transactional
    public void addRole(String userId, String[] ids) {
        for (String id : ids) {
            usersInfo.addRole(userId,id);
        }
    }


}
